package cn.ad.server.domain.ad;

import org.joda.time.DateTime;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;

public class Campaign implements Serializable {

	private static final long serialVersionUID = 5751730484103279550L;

	static final String VERSION = "0";
	static final String CACHE_NAME = "campaign";

	private int id;
	private String name;
	private Date startTime;
	private Date endTime;
	private Advertiser advertiser;
	private int status;
	private Date createTime;
	private Date updateTime;
	private String description;
	private Industry industry;
	private BigDecimal unitPrice;
	private int feeType;
	private BigDecimal budget;
	private BigDecimal budgetDay;
	private int maxImpressions;
	private int dayImpressions;
	private int maxClicks;
	private int dayClicks;
	private Set<Creative> creatives;

	public static String cacheKey(long id) {
		return new StringBuilder(CACHE_NAME).append(":").append(VERSION).append(":").append(id).toString();
	}

	public String toCacheKey() {
		return new StringBuilder(CACHE_NAME).append(":").append(VERSION).append(":").append(this.getId()).toString();
	}

	@Deprecated
	public String maxImpressionsKey() {
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append("max.impressions")
				.toString();
	}

	public static String maxImpressionsKey(int id) {
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append("max.impressions").toString();
	}

	/**
	 * 返回印象缓存过期时间 单位（小时）
	 * @return
	 * @deprecated 改为传递参数的静态方法实现, {@link #maxImpressionsExpireHour(long)}
	 */
	@Deprecated
	public long maxImpressionsExpireHour() {
		if (this.getEndTime() == null)
			return 0;
		long dif = this.getEndTime().getTime() - System.currentTimeMillis();
		return dif / 1000 / 3600 + 1;
	}

	/**
	 * 返回印象缓存过期时间 单位（小时）
	 * @return
	 */
	public static long maxImpressionsExpireHour(long endTimeSec) {
		if (endTimeSec == 0)
			return 0;
		long dif = endTimeSec - System.currentTimeMillis();
		return dif / 1000 / 3600 + 1;
	}

	/**
	 * 获取每日印象数缓存KEY
	 * @return
	 * @deprecated {@link Campaign#dayImpressionsKey(int)}
	 */
	@Deprecated
	public String dayImpressionsKey() {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date).append(":")
				.append("day.impressions").toString();
	}

	/**
	 * 获取每日印象数缓存KEY
	 * @param id
	 * @return
	 */
	public static String dayImpressionsKey(int id) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":")
				.append("day.impressions").toString();
	}

	/**
	 * @return
	 * @deprecated {@link #maxClicksKey(int)}
	 */
	@Deprecated
	public String maxClicksKey() {
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append("max.clicks")
				.toString();
	}

	public static String maxClicksKey(int id) {
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append("max.clicks").toString();
	}

	/**
	 * @return
	 * @deprecated {@link #maxClicksExpireHour(long)}
	 */
	@Deprecated
	public long maxClicksExpireHour() {
		if (this.getEndTime() == null)
			return 0;
		long dif = this.getEndTime().getTime() - System.currentTimeMillis();
		return dif / 1000 / 3600 + 1;
	}

	public static long maxClicksExpireHour(long endTimeSec) {
		if (endTimeSec == 0)
			return 0;
		long dif = endTimeSec - new Date().getTime();
		return dif / 1000 / 3600 + 1;
	}

	@Deprecated
	public String dayClicksKey() {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date).append(":")
				.append("day.clicks").toString();
	}

	public static String dayClicksKey(long id) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":")
				.append("day.clicks").toString();
	}

	@Deprecated
	public String maxBudgetKey() {
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append("max.budget")
				.toString();
	}

	public static String maxBudgetKey(long id) {
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append("max.budget").toString();
	}

	@Deprecated
	public long maxBudgetExpireHour() {
		if (this.getEndTime() == null)
			return 0;
		long dif = this.getEndTime().getTime() - System.currentTimeMillis();
		return dif / 1000 / 3600 + 1;
	}

	public static long maxBudgetExpireHour(long endTimeSec) {
		if (endTimeSec == 0)
			return 0;
		long dif = endTimeSec - System.currentTimeMillis();
		return dif / 1000 / 3600 + 1;
	}

	@Deprecated
	public String dayBudgetKey() {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date).append(":")
				.append("day.budget").toString();
	}

	public static String dayBudgetKey(long id) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":")
				.append("day.budget").toString();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Advertiser getAdvertiser() {
		return advertiser;
	}

	public void setAdvertiser(Advertiser advertiser) {
		this.advertiser = advertiser;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Industry getIndustry() {
		return industry;
	}

	public void setIndustry(Industry industry) {
		this.industry = industry;
	}

	public BigDecimal getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}

	public int getFeeType() {
		return feeType;
	}

	public void setFeeType(int feeType) {
		this.feeType = feeType;
	}

	public BigDecimal getBudget() {
		return budget;
	}

	public void setBudget(BigDecimal budget) {
		this.budget = budget;
	}

	public BigDecimal getBudgetDay() {
		return budgetDay;
	}

	public void setBudgetDay(BigDecimal budgetDay) {
		this.budgetDay = budgetDay;
	}

	public int getMaxImpressions() {
		return maxImpressions;
	}

	public void setMaxImpressions(int maxImpressions) {
		this.maxImpressions = maxImpressions;
	}

	public int getDayImpressions() {
		return dayImpressions;
	}

	public void setDayImpressions(int dayImpressions) {
		this.dayImpressions = dayImpressions;
	}

	public int getMaxClicks() {
		return maxClicks;
	}

	public void setMaxClicks(int maxClicks) {
		this.maxClicks = maxClicks;
	}

	public int getDayClicks() {
		return dayClicks;
	}

	public void setDayClicks(int dayClicks) {
		this.dayClicks = dayClicks;
	}

	public Set<Creative> getCreatives() {
		return creatives;
	}

	public void setCreatives(Set<Creative> creatives) {
		this.creatives = creatives;
	}

}
